package com.galo.service;

import com.galo.service.config.MqttProperties;
import com.github.wxiaoqi.security.auth.client.EnableAceAuthClient;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@SpringBootApplication
@Configuration
@EnableEurekaClient
@EnableFeignClients({"com.github.wxiaoqi.security.auth.client.feign"})
@EnableAceAuthClient
@EnableConfigurationProperties(MqttProperties.class)
@EnableSwagger2Doc
public class ServiceApplication {

    /**
     * 异步线程池
     */
    @EnableAsync
    @Configuration
    class TaskPoolConfig{

        @Bean("taskExecutor")
        public Executor taskExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            //核心线程数
            executor.setCorePoolSize(10);
            //最大线程数
            executor.setMaxPoolSize(20);
            //缓冲队列
            executor.setQueueCapacity(200);
            //允许线程的空闲时间
            executor.setKeepAliveSeconds(60);
            //线程池名的前缀
            executor.setThreadNamePrefix("taskExecutor-");
            //线程池对拒绝任务的处理策略
            executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
            return executor;
        }
    }

    /**
     * 启动引导主函数
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }

}
